package com.coco.bargain.service.impl;

import com.coco.bargain.mapper.BargainGoodsMapper;
import com.coco.bargain.mapper.BargainItemMapper;
import com.coco.bargain.pojo.TbBargainGoods;
import com.coco.bargain.pojo.TbBargainItem;
import com.coco.bargain.service.BargainGoodsService;
import com.coco.bargain.vo.BargainGoodsVo;
import com.coco.bargain.vo.GoodsSearchVo;
import com.coco.common.result.ResultData;
import com.coco.common.util.IdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author minicoco
 * @version 1.0
 * @date 2021/7/21 20:08
 */
@Service
public class BargainGoodsServiceImpl implements BargainGoodsService {

    @Autowired
    private BargainGoodsMapper bargainGoodsMapper;

    @Autowired
    private BargainItemMapper bargainItemMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public ResultData findByStatusAndName(GoodsSearchVo searchVo) {
        PageHelper.startPage(searchVo.getPage(), searchVo.getLimit());
        List<TbBargainGoods> bargainGoodsList = bargainGoodsMapper.search(searchVo);
        PageInfo<TbBargainGoods> pageInfo = new PageInfo<>(bargainGoodsList);
        return new ResultData(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public void deleteById(Long id) {
        bargainGoodsMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        bargainGoodsMapper.updateStatus(id,status);
    }

    @Override
    public TbBargainGoods findById(Long id) {
        return bargainGoodsMapper.findById(id);
    }

    @Override
    public void updateById(TbBargainGoods bargainGoods) {
        bargainGoodsMapper.updateById(bargainGoods);
    }

    @Override
    public void addToTbBargainGoods(TbBargainGoods tbBargainGoods) {
        bargainGoodsMapper.addToTbBargainGoods(tbBargainGoods);
    }

    /**
     * spu与sku添加
     * @author HanSama
     * @date 2021/7/22 19:30
     * @param bargainGoodsVo
     */
    @Override
    public void addTbBargainGoods(BargainGoodsVo bargainGoodsVo) {
        //spu
        TbBargainGoods spu = bargainGoodsVo.getBargainGoods();
        //sku List
        List<TbBargainItem> skuList = bargainGoodsVo.getBargainItemList();
        //设置spu砍价价格 为所有sku最小值
        spu.setBargainPrice(skuList.stream().map(TbBargainItem::getBargainPrice).min((e1, e2)->e1.compareTo(e2)).get());
        //设置sku限量剩余
        skuList = skuList.stream().map((sku) -> {
            sku.setResidueNum(sku.getLimitNum());
            return sku;
        }).collect(Collectors.toList());
        //设置spu限量
        spu.setLimitNum(skuList.stream().mapToInt(TbBargainItem::getLimitNum).sum());
        //设置spu限量剩余
        spu.setResidueNum(skuList.stream().mapToInt(TbBargainItem::getResidueNum).sum());
        //添加spu砍价商品
        bargainGoodsMapper.addToTbBargainGoods(spu);
        //获取spu自增id
        Long spuId = spu.getId();
        //添加sku 设置sku的spuID
        skuList.forEach(sku->{
            sku.setBargainGoodsId(spuId);
            bargainItemMapper.addBargainItem(sku);
        });

    }

    /**
     * 通过spuID获取相关sku 并全部发返回
     * @author HanSama
     * @date 2021/7/22 19:41
     * @param id
     * @return com.coco.bargain.vo.BargainGoodsVo
     */
    @Override
    public BargainGoodsVo findOne(Long id) {
        //通过id获取砍价spu
        TbBargainGoods sup = bargainGoodsMapper.findById(id);
        //通过spuID获取相关sku
        List<TbBargainItem> skuList =  bargainItemMapper.findBargainItemListByBargainGoodsId(id);
        //封装并返回结果
        return new BargainGoodsVo(sup,skuList);
    }

    @Override
    public void updateBargainGoods(BargainGoodsVo bargainGoodsVo) {
        //spu
        TbBargainGoods spu = bargainGoodsVo.getBargainGoods();
        //sku List
        List<TbBargainItem> skuList = bargainGoodsVo.getBargainItemList();
        //设置spu砍价价格 为所有sku最小值
        spu.setBargainPrice(skuList.stream().map(TbBargainItem::getBargainPrice).min((e1, e2)->e1.compareTo(e2)).get());
        //设置sku限量剩余
        skuList = skuList.stream().map((sku) -> {
            sku.setResidueNum(sku.getLimitNum());
            return sku;
        }).collect(Collectors.toList());
        //设置spu限量
        spu.setLimitNum(skuList.stream().mapToInt(TbBargainItem::getLimitNum).sum());
        //设置spu限量剩余
        spu.setResidueNum(skuList.stream().mapToInt(TbBargainItem::getResidueNum).sum());

        //修改spu
        bargainGoodsMapper.updateById(spu);
        //修改sku
        skuList.forEach(sku ->{
            bargainItemMapper.updateBargainItem(sku);
        });

    }


}
